package fr.umlv.android.caveAVin.activities;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import fr.umlv.android.caveAVin.activities.R;
import fr.umlv.android.caveAVin.activities.R.id;
import fr.umlv.android.caveAVin.activities.R.layout;
import fr.umlv.android.caveAVin.beans.User;
import fr.umlv.android.caveAvin.sqlite.InitBDD;
import fr.umlv.android.caveAvin.sqlite.MyProjectBDD;
import fr.umlv.android.caveAvin.sqlite.UserBDD;

public class AuthentificationMenu extends Activity {
	
	EditText loginField;
	EditText passWordField;
	Button authentificationOK;
	Button createBouton;
	TextView errorAuthentification;
	
	public static boolean initBDD = true;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.authentification_menu);
        loginField = (EditText) findViewById(R.id.Login);
        passWordField = (EditText) findViewById(R.id.PassWord);
        authentificationOK = (Button) findViewById(R.id.AuthentificationOK);
        createBouton = (Button) findViewById(R.id.createUser);
        errorAuthentification = (TextView) findViewById(R.id.ErrorAuthentification);
        
        Intent myLocalIntent = getIntent();
//		// look into the bundle sent to Activity2 for data items
		Bundle myBundle = myLocalIntent.getExtras();
		
		
//		if (myBundle != null) {
//			initBDD  = myBundle.getBoolean("initBDD");
//		}
		
		if (initBDD) {
			initBDD = false;
			// initialisation de la base.
			final MyProjectBDD myProjectBDD = new MyProjectBDD(this);
			myProjectBDD.open();
			InitBDD.run();
		}
		
        
        
        
        
        authentificationOK.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String login = loginField.getText().toString();
				String passWord = passWordField.getText().toString();
				
				if( login.equals("") || passWord.equals("")) {
					errorAuthentification.setText("Authentication Error, login or passWord is NULL !!!");
				}
				else {
					errorAuthentification.setText("");
					User user =new User();
					user = UserBDD.getUserWithLoginPass(login, passWord);
					if (user == null) {
						errorAuthentification.setText("Login, pass incorrect !!! ");
					} else {
						// create intent to call Activity2
						Intent myIntentAuthentificationMenuToMainMenu = new Intent(AuthentificationMenu.this,MainMenu.class);
						// create a container to ship data
						Bundle myData = new Bundle();
						// add <key,value> data items to the container
						myData.putString("currentLogin", login);
						//attach currect id User
						myData.putInt("idCurrentUser", user.getId());
						// attach the container to the intent
						myIntentAuthentificationMenuToMainMenu.putExtras(myData);
						// call Activity2, tell your local listener to wait response
						startActivity(myIntentAuthentificationMenuToMainMenu);
					}
				}
			}
		});
        
        createBouton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent myIntentAuthentificationToCreateUser = new Intent(AuthentificationMenu.this,CreateUser.class);
				startActivity(myIntentAuthentificationToCreateUser);
			}
		});
    }
}